<?php

namespace Helper\Controller;

use Common\Controller\ApiController;
use Util\StringUtils;

class SettingController extends ApiController {

    public function getMoneyRate() {
        $rate = D('Setting')->getByK('moneyrate');
        $value = unserialize($rate['v']);
        $value['money_denominations'] = explode('|', $value['money_denominations']);
        $value['coin_denominations'] = explode('|', $value['coin_denominations']);
        $this->response($value);
    }

    public function getSite() {
        $site = D('Setting')->field('v')->getByK('site');
        $value = mb_unserialize($site['v']);
        $this->response($value);
    }

    public function getProtocol() {
        $protocol = D('Setting')->cache(365 * 24 * 60 * 60)->getByK('protocol');
        $protocol = unserialize($protocol['v'])['protocol'];
        $protocol = html_entity_decode($protocol);
//        $userprotocol = D('Setting')->cache(365 * 24 * 60 * 60)->getByK('userprotocol');
//        $userprotocol = unserialize($userprotocol['v'])['userprotocol'];
//        $userprotocol = html_entity_decode($userprotocol);
        $data['protocol'] = $protocol;
//        $data['userprotocol'] = $userprotocol;
        $this->response($data);
    }

    /*     * 获取分享数据* */

    public function share() {
        $uid = $this->user['id'];
//        $url = D('Wechat')->getField('weburl');
        $uid = $uid ? $uid : 0;
        $share['title'] = '聊吧-来这里畅谈自己';
        $share['desc'] = '一个优质,微众人群聊天聚焦的地方，说唱自如，尽享快乐';
        $share['cover'] = "http://".$_SERVER['SERVER_NAME']."/logo.png";
        $share['url'] = "http://{$_SERVER['SERVER_NAME']}/share.html?id={$uid}";
        $this->response($share);
    }

    /*     * 分享回来后的请求，暂时不用，因为任务id:7去掉了，改为邀请好友注册成功后送300书币* */

    public function doTaskShare() {
        $uid = $this->user['id'];
        D('Task/task')->taskDayRedis($uid, 'task_day', 7); //1是更新个人信息的任务
        $this->createdResponse();
    }

    public function countShare() {
        $uid = $this->user['id'];
        $data['count'] = D('User')->where(['shareid' => $uid])->count();
        $data['gold'] = $data['count'] * 300;
        $this->response($data);
    }

    public function shareRegister() {
        if (!empty(I('get.data'))) {
            $data = I('get.data');
            $password = $data['password'];
            $mobile = $data['mobile'];
            $mobileCode = $data['mobile_code'];
            $sk = $data['id'];
            /* 验证手机验证码 */
            if (!$this->checkMobileCode($mobile, $mobileCode)) {
                $data = ['code' => 404, 'message' => '验证码不对'];
                $this->ajaxReturn($data, 'json');
            }

            $userModel = D('Account/User');

            if (!empty($mobile)) {
                $user = $userModel->where(['mobile' => $mobile])->count(); //验证手机是否存在
                if ($user > 0) {
                    $data = ['code' => 404, 'message' => '该帐号已注册'];
                    $this->ajaxReturn($data, 'json');
                }
            }
            try {
                $user = $userModel->register($mobile, $password, $mobile, 'https://jinbanbook.oss-cn-shenzhen.aliyuncs.com/default.png', 1, $sk);
                $Model = M();
                $Model->startTrans();
                //分享任务功能
                $res1 = D('User')->where(['id' => $sk])->setInc('gold', 300);
                $res2 = D('Account/GoldLog')->addGoldLog(['user_id' => $sk, 'mark' => '每邀请一位好友注册', 'gold' => 300, 'type' => 'send', 'add_time' => time()]);
                if (!empty($res1) && !empty($res2)) {
                    D('Account/Message')->addMessage('分享好友注册奖励金币', "亲爱的会员用户，由于您分享金榜藏书APP获得好友注册下载，我们将赠送您300书币", $sk, 3);
                    $Model->commit();
                    $data = ['code' => 200, 'message' => '注册成功'];
                    $this->ajaxReturn($data, 'json');
                } else {
                    $Model->rollback();
                }
            } catch (ModelException $e) {
                $data = ['code' => 404, 'message' => '注册失败'];
                $this->ajaxReturn($data, 'json');
            }
        } else {
            $id = I('get.id');
            $token = StringUtils::keyGen();
//            $tokenkey = strrev($token);
//            S($tokenkey,$token);
//            $wechat = D('Wechat')->find(1);
            $data['android'] = $wechat['androiddurl']; //android下载地址
            $data['ios'] = $wechat['iosdurl']; //ios下载
            $this->assign('data', $data);
            $this->assign('token', $token);
            $this->assign('id', $id);
            $this->display('index');
        }
    }
    
    /*
     * 分享满地页
     */
    
}
